package seotools.positions.direct;

import data.DataHelper;
import data.ExtramobiExcel;
import data.ExtramobiNew;
import data.ItemData;
import jxl.read.biff.BiffException;
import obj.Item;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class YandexMarket {
    public static final String SEP = ",";
    private static final String BASE_URL = "http://extramobi.ru/product/accumulator_";
    public static final String BASE_PICTURE_URL = "http://extramobi.ru/published/publicdata/EXTRAMOBI/attachments/SC/products_pictures/";
    private static final int DELIVERY_COST = 250;
    private static Map<String, String> commonProps;

    public static int count = 0;

    public static void main(String[] args) throws IOException, BiffException {
        commonProps = ExtramobiNew.readCommonProps();
        PrintStream out = new PrintStream(new FileOutputStream("yandex.market.csv"), false, "UTF-8");
        out.println(MarketCSV.getHeader());
        addAccum(AdType.ACCUM, out, "Nokia", "Samsung", "Sony Ericsson", "HTC", "Apple");
    }

    private static void addAccum(AdType adType, PrintStream out, String... brands) throws IOException, BiffException {
        String inFile = adType.file;
        Map<String, List<ItemData>> items = ExtramobiExcel.parseItems(inFile, new ArrayList<String>(), false, brands);
        for (String brandFile : items.keySet()) {
            List<String> processedModels = new ArrayList<String>();
            for (ItemData item : items.get(brandFile)) {
                if (item.count < 100) {
                    System.out.println("Rejected by count " + item.name);
                    continue;
                }
                MarketCSV marketCSV = new MarketCSV();
                marketCSV.id = count++;
                marketCSV.url = commonProps.get("common.product-url") + item.url;
                marketCSV.price = item.price;
//                marketCSV.picture = (BASE_PICTURE_URL + imagePrefix + item.name.toLowerCase() + "-large.jpg").replaceAll(" ", "%20");
                marketCSV.picture = BASE_PICTURE_URL + item.photos.get(0).split(",")[2];
                marketCSV.vendor = item.brand;
                marketCSV.name = item.name.replace("оригинальный ", "");
                marketCSV.desc = "Аккумулятор для мобильных телефонов " + item.brand + " " + item.model + ". Совместимые модели " + marketCSV.vendor + ": ";
                for (String model : item.models) {
                    String newDesc = marketCSV.desc + model + ";";
                    if (newDesc.length() > 512) {
                        break;
                    }
                    marketCSV.desc = newDesc;
                }
                if (marketCSV.name.length() > 255) {
                    throw new IllegalStateException("превышена длина " + marketCSV.name + "," + marketCSV.name.length());
                }
                if (marketCSV.desc.length() > 512) {
                    throw new IllegalStateException("превышена длина " + marketCSV.desc + "," + marketCSV.desc.length());
                }
                out.println(marketCSV);
                int i = 0;
                for (String model : item.models) {
                    if (processedModels.contains(model)) {
                        System.out.println("Already processed: " + model);
                        continue;
                    }
                    marketCSV.id = count++;
                    marketCSV.name = "Аккумулятор " + item.brand + " " + model;
                    marketCSV.url = commonProps.get("common.product-url") + ExtramobiExcel.replaceModelUrl(item, model, item.url);
                    marketCSV.desc = "Аккумулятор для мобильного телефона " + item.brand + " " + model + ".";
                    out.println(marketCSV);
                    processedModels.add(model);
                }
            }
        }
    }

    static class MarketCSV {
        int id;
        String name;
        boolean available = true;
        String url;
        int price;
        String currId = "RUR";
        String category = "Телефоны/Аксессуары для сотовых/Аккумуляторы";
        String picture;
        int deliveryLocalCost = DELIVERY_COST;
        String vendor;
        String desc;
        boolean delivery = true;
        boolean store = true;
        boolean pickup = true;

        @Override
        public String toString() {
            return id +
                    SEP + name +
                    SEP + available +
                    SEP + url +
                    SEP + price +
                    SEP + currId +
                    SEP + category +
                    SEP + picture +
                    SEP + delivery +
                    SEP + deliveryLocalCost +
                    SEP + vendor +
                    SEP + desc +
                    SEP + store +
                    SEP + pickup;
        }

        public static String getHeader() {
            return "id" +
                    SEP + "name" +
                    SEP + "available" +
                    SEP + "url" +
                    SEP + "price" +
                    SEP + "currencyId" +
                    SEP + "category" +
                    SEP + "picture" +
                    SEP + "delivery" +
                    SEP + "local_delivery_cost" +
                    SEP + "vendor" +
                    SEP + "description" +
                    SEP + "store" +
                    SEP + "pickup";
        }
    }
}
